package com.ruoyi.web.controller.custom;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RnxInviteLog;
import com.ruoyi.system.domain.RnxRoster;
import com.ruoyi.system.service.IRnxInviteLogService;
import com.ruoyi.system.service.IRnxRosterService;
import com.ruoyi.web.utils.EmailSender;
import com.ruoyi.web.utils.EmailTemplateEnum;
import com.ruoyi.web.utils.TimeFormatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @ClassName RnxRegisterController
 * @Description 瑞念信 注册
 * @Author yangtao2
 * @Date 2022/4/29 14:17
 * @Version 1.0
 **/
@Slf4j
@Controller
@RequestMapping(value = "/rnx")
public class RnxRegisterController {

	private String prefix = "rnx/register/";

	@Autowired
	private EmailSender mailSender;

	@Autowired
	private IRnxRosterService rnxRosterService;

	@Autowired
	private IRnxInviteLogService rnxInviteLogService;

	/**
	 * 邀请完善员工信息
	 * @return
	 */
	@GetMapping(value = "/register")
	String register(){
		return prefix+"invite";
	}

	/**
	 * 校验手机号
	 * @param phone
	 * @return
	 */
	@PostMapping(value = "/register/check")
	@ResponseBody
	AjaxResult check(@RequestParam(value = "phone") String phone){
		log.info("/rnx/register/check in: " + phone);
		//查询邀请记录
		RnxInviteLog inviteLog = rnxInviteLogService.selectRnxInviteLogByPhone(phone);
		if(inviteLog == null){
			return AjaxResult.error("无邀请记录,请联系HR!");
		}
		//核验是否已经填写过
		if(inviteLog.getState() == 2L){
			return AjaxResult.error("您已经填写过了,如需修改,请联系HR!");
		}
		log.info("/rnx/register/check success: " + phone);
		return AjaxResult.success();
	}

	/**
	 * 获取填写信息页面
	 * @return
	 */
	@GetMapping(value = "/register/add/{phone}")
	String add(@PathVariable(value = "phone") String phone, ModelMap map){
		log.info("新员工填写信息:"+phone);
		if(StringUtils.isNotBlank(phone)) {
			//查询基本信息
			RnxInviteLog inviteLog = rnxInviteLogService.selectRnxInviteLogByPhone(phone);
			//填充log
			map.put("inviteLog", inviteLog);
		}
		return prefix + "register";
	}

	/**
	 * 更新信息
	 * @param roster
	 * @return
	 */
	@PostMapping(value = "/register/update")
	@Log(title = "员工花名册", businessType = BusinessType.UPDATE)
	@ResponseBody
	AjaxResult update(RnxRoster roster){
		//提交再次校验
		String phone = roster.getMobilePhone();
		RnxInviteLog inviteLog = rnxInviteLogService.selectRnxInviteLogByPhone(phone);
		if(inviteLog == null){
			return AjaxResult.error("无邀请记录,请联系HR!");
		}
		//核验是否已经填写过
		if(inviteLog.getState() == 2L){
			return AjaxResult.error("您已经填写过了,如需修改,请联系HR!");
		}
		//更新信息
		int i = rnxRosterService.updateRnxRoster(roster);
		if(i <= 0){
			//新增信息
			i = rnxRosterService.insertRnxRoster(roster);
		}
		if(i > 0){
			//设置状态
			inviteLog.setState(2L);
			//设置名称
			inviteLog.setFullName(roster.getFullName());
			//设置邮箱
			inviteLog.setEmail(roster.getMailbox());
			inviteLog.setExt(TimeFormatUtils.getNowWithYMDHMS());
			rnxInviteLogService.updateRnxInviteLog(inviteLog);
		}else{
			return AjaxResult.error("信息录入失败,请联系HR!");
		}
		//发送邮件
		mailSender.send(
				EmailTemplateEnum.NEW_USER_COMPLETE_INFORMATION_ENTRY,
				new HashMap(2) {{
					put("user", roster.getFullName());
					put("date", TimeFormatUtils.getNowWithYMDHMS());
				}});
		return AjaxResult.success("信息录入成功!");
	}


}
